#include "strops.h"


namespace so{

    bool    strbegin(const char* content, const char* strset)
    {
        const char* pr = ::strstr(content, strset);
        if( pr == (char*)content)
            return 1;
        char locoset[128];
        ::strcpy(locoset, (char*)strset);
        strupper(locoset);
        return ::strstr(content, locoset) == content;
    }

    const char*   stristr(const char* content, const char* strset)
    {
        const char* ret = ::strstr(content, strset);
        if(ret)
            return ret;
        char locoset[1024];
        ::strcpy(locoset, (char*)strset);
        strupper(locoset);
        return ::strstr(content, locoset);
    }
    const char*   strstr(const char* content, const char* strset)
    {
        const char* ret = ::strstr(content, strset);
        if(ret)
            return ret;
        char locoset[512];
        ::strcpy(locoset, (char*)strset);
        strupper(locoset);
        return ::strstr(content, locoset);
    }
    void   trimcrlf(char* p)
    {
        int   d  = 0;
        char* pd = p;
        while(*p)
        {
            if(*p!='\r' && *p!='\n')
            {
                pd[d++]=*p;
            }
            ++p;
        }
        pd[d++]=0;
    }

    void strlower(char* a)
    {
	    char* pc = a;
        int    diff = 'a'-'A';
		while(*pc)
		{

			if(*pc>='A'&& *pc<='Z')
				*pc += diff;
			++pc;
		}
    }

	void strupper(char* a)
	{
	    char* pc = a;
        int diff = 'a'-'A';
		while(*pc)
		{
			if(*pc>='a'&& *pc<='z')
				*pc -= diff;
			++pc;
		}

	}

    bool isdigit(char c)
    {
        return (c >= '0' && c <= '9');
    }
    char *strnew(const char *psz)
    {
        char *res = new char[strlen(psz) + sizeof(char)];
        ::strcpy(res, (char*)psz);
        return res;
    }
    int chartohex (char c)
    {
        if (c >= '0' && c <= '9')
            return (c - '0');
        else if (c >= 'a' && c <= 'f')
            return (c - 'a' + 10);
        else if (c >= 'A' && c <= 'F')
            return (c - 'A' + 10);
        else
            return -1;
    }


    char* strlastchr(char* s, char p)
    {
        char* pdot = s;
        char* pdp = pdot;
        while(1){
            pdot = pdp;
            pdp = strchr(pdot,p);
            if(pdp)++pdp;
            else break;
        }
        return pdot-1;
    }

    char* strprevchr(char* start, char* s, char p)
    {
        char* cp = s;
        while(cp!=start && *cp != p)--cp;
        return cp;
    }

    char* str_chr(char* s, char p)
    {
        while(*s && *s!=p)++s;
        return s;
    }

    int   str_count(const char* s, char cc)
    {
        int c = 0;
        const char* p = s;
        while(*p)if(*p++==cc)++c;
        return c;
    }

    char* strnchr(char* s, char p, int pos)
    {
        char* pdot = s;
        char* pdp = pdot;
        while(1){
            pdot = pdp;
            pdp = strchr(pdot,p);
            if(pdp && --pos)
                ++pdp;
            else
                break;
        }
        return pdp ? pdp : pdot-1;
    }

    const char* strccpy(char* d, const char* s, char occ)
    {
        while(*s!=occ && *s)
            *d++=*s++;
        *d = 0;
        return s;
    }

    char* strscpy(char* d, char* s, const char* occ)
    {
        while(*s && !strchr(occ,*s))
            *d++=*s++;
        *d=0;
        return s;
    }

    bool is_alnum(const char c)
    {
        return  (c>='0' && c<='9') ||  (c<='z' && c>='A');
    }

    char* strskipalnum(char *s)
    {
        while(*s && *s>0 && is_alnum(*s))++s;
        return s;
    }

    bool  strcat(char* d, char*s, int szl)
    {
        if((strlen(d) + strlen(s)) < (size_t)(szl-1))
        {
            ::strcat(d,s);
            return 1;
        }
        return 0;
    }

    void  keep_alphanum(char* p)
    {
        int   d  = 0;
        char* pd = p;
        while(*p)
        {
            if(is_alnum(*p))
            {
                pd[d++]=*p;
            }
            ++p;
        }
        pd[d++]=0;
    }

	void replace_urlfmt(char* p)
	{
	    while(*p)
        {
            if(*p=='%')
				*p='_';
            ++p;
        }
        *p=0;
	}

    void  format_ascii(char* p){
        while(*p)
        {
            if(*p=='/')
                *p='(';
			else if(*p=='\\')
				*p=')';
            else if(*p=='?')
                *p='[';
            else if(*p=='*')
                *p='^';
            else if(*p=='&')
                *p=',';
            else if(*p=='\"')
                *p='\'';
			else if(*p=='&')
				*p='+';
            ++p;
        }
        *p=0;
    }

    char* trimfront(char* p)
    {
        while(*p && (*p==' ' || *p=='\t'))
            ++p;
        return p;
    }

    void   trimrightsp(char* p)
    {
        char* plast = p+strlen(p)-1;
        while(plast!=p && (*plast==' '||*plast=='\t'))
            --plast;
        *(plast+1)=0;
    }

    void  trimright(char* p, char c)
    {
        char* plast = p+strlen(p)-1;
        while(plast!=p && *plast==c)
            --plast;
        *(plast+1)=0;
    }

    void  trimeol(char* p)
    {
        char* plast = p+strlen(p)-1;
        while(plast!=p && (*plast=='\r' || *plast=='\n'))
            --plast;
        *(plast+1)=0;
    }


    void  trimnonsql(char* p)
    {
        char *d=p;

        while(*p)
        {
            if(*p==',')
                *d++=' ';
            else if((*p >= 'a' && *p <= 'z') || (*p >='A' && *p<= 'Z'))
            {
                *d++=*p;
            }
            ++p;
        }
        *d=0;
    }

    void  trimall(char* p, char c)
    {
        char *d=p;
        while(*p)
        {
            if(*p!=c)
            {
                *d++=*p;
            }
            ++p;
        }
        *d=0;
    }

    char* strcpy(char* d, char* s, size_t destsz)
    {
        if(destsz){
            while(*s && --destsz)
                *d++=*s++;
            *d=0;
        }
        else
        {
            while(*s)
                *d++=*s++;
            *d=0;
        }
        return d;
    }

    bool strend(const char* hay, const char* ned)
    {
        size_t hl = strlen(hay);
        size_t ed = strlen(ned);
        if(hl > ed)
        {
            hay += (hl-ed);
            return !::strcmp(hay, ned);
        }
        return 0;
    }

    void    manglename(char* p)
    {
        while(*p)
        {
            if(*p=='?')
                *p='#';
            ++p;
        }
        *p = 0;
    }

    int  str_findlast(const char* a, char chr)
    {
        int pla = (int)strlen(a);
        while(--pla>-1)
        {
            if(a[pla]==chr)
                return (int)pla;
        }
        return 0;
    }

};
